<?php

namespace app\ctrl;

use app\common\lib\Helper;
use app\service\Lottery;
use app\service\MongoHelper;
use core\lib\Config;
use core\lib\Log;

class TestCtrl extends \core\Base
{
    public function try()
    {
        try {
            echo 111;
            throw new \Exception('not is array');
            return true;
        } catch (\Exception $e) {
            pp(222, $e->getMessage());
        } finally {
            echo 'finally';
        }
    }

    public function uuid()
    {
        $m = (float)sprintf("%.0f", microtime(true) * 1000);
        p($m, strlen($m));
        $p2 = new \app\common\lib\Snowflake();
        $p2->getInstance(2, 5);
        $guid = $p2->nextId();
        p($guid, strlen($guid));
    }

    public function uuidRedis()
    {
        $instance = \app\common\lib\SnowflakeRedis::getInstance();
        $instance->setWorkerId(2)->setServerId(1);
        $number = $instance->nextId();
        echo "number: $number, len: " . strlen($number) . "\n";

        //$item = $instance->reverseNumber($number);
        //var_dump($item);
    }

    public function lottery()
    {
        echo json_encode((new Lottery())->get_prize(), JSON_UNESCAPED_UNICODE);
    }

    public function mongo()
    {
        echo phpinfo();die;
//        $data = [
//            ['x' => 1, 'name' => 'u1', 'age' => '14'],
//            ['x' => 2, 'name' => 'u2', 'age' => '24'],
//            ['x' => 3, 'name' => 'u3', 'age' => '18'],
//            ['x' => 4, 'name' => 'u4', 'age' => '30'],
//            ['x' => 5, 'name' => 'u5', 'age' => '22'],
//        ];
//        $result = MongoHelper::getInstance()->namespaces("test.sites")
//            ->insert($data);
//        pp($result);

        // 常规查询
        $result = MongoHelper::getInstance()->namespaces("test.sites")
            ->whereIn("name", ["u1", "u5"])->order("age", MongoHelper::SORT_DESC)->field("name,age")
            ->find();
        p($result);

        //聚合查询
        $aggregateRes = MongoHelper::getInstance()->namespaces("test.sites")
            ->whereIn("name", ["u1", "u5"])->max("age");
        pp($aggregateRes);
    }

    public function getImgInfo() {
        $url = 'https://resource.hypernft.world/nft/ssr/SSR11.png';
        $url = 'https://resource.hypernft.world/hypernft/11.png';
        $startTime = microtime(true);
        $result = @fopen($url, 'r');
        $runTime = microtime(true) - $startTime;
        echo "fopen: " . ($result ? '文件存在' : '文件不存在') . ", 耗时：$runTime</br>";

        // -----------------------
        $startTime = microtime(true);
        $result = @getimagesize($url);
        $runTime = microtime(true) - $startTime;
        echo "getimagesize: " . ($result ? '文件存在' : '文件不存在') . ", 耗时：$runTime</br>";
        //p($result);

        // ----------------
        /*$startTime = microtime(true);
        // 不能用于远程文件资源
        $result = file_exists($imgSrc);
        $runTime = microtime(true) - $startTime;
        echo "file_exists: " . ($result ? '文件存在' : '文件不存在') . ", 耗时：$runTime</br>";*/

        // -------------
        $startTime = microtime(true);
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $contents = curl_exec($ch);
        curl_close($ch);
        $runTime = microtime(true) - $startTime;
        //echo $contents;
        $result = preg_match("/403/", $contents);
        echo "curl: " . ($result ? '文件不存在' : '文件存在') . ", 耗时：$runTime</br>";

        // -------------------
        $startTime = microtime(true);
        $curl = curl_init($url);
        // 不取回数据/ 不下载
        curl_setopt($curl, CURLOPT_NOBODY, true);
        // 发送请求
        $result = curl_exec($curl);
        //var_dump($result);
        $runTime = microtime(true) - $startTime;
        $found = false;
        // 如果请求没有发送失败
        if ($result !== false) {
            // 再检查http响应码是否为200
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($statusCode == 200) {
                $found = true;
            }
        }
        curl_close($curl);
        echo "curl2: " . ($found ? '文件存在' : '文件不存在') . ", 耗时：$runTime</br>";

        /*文件存在时：
            fopen: 文件存在, 耗时：0.80981397628784
            curl: 文件存在, 耗时：3.1059420108795
            curl2: 文件存在, 耗时：0.64614486694336
        */


        return true;
    }

    public function read() {
        $url = 'https://resource.hypernft.world/hypernft/1.png';
        $arr = parse_url($url);
        $arr2 = pathinfo($arr['path']);
        $filename = basename($url); // 1.png
        p($arr, $arr2, $filename);

        $file = ROOT . 'runtime/gitInfoCheckError.log';
        $data1 = Helper::readLog($file);
        p(json_encode($data1),$data1);
        //$data1[0] = 'https://resource.hypernft.world/hypernft/126514.jng';
        $file2 = ROOT . 'runtime/21_cli.log';
        $data2 = Helper::readLog2($file2);
        $diff = array_diff($data1, $data2); // array_diff_assoc() array_diff_uassoc()
        $diff2 = array_diff($data2, $data1);
        p($diff, 22, $diff2, $data1, $data2);
    }
}